<template>
  <div v-drag>
    <div class="mask"  @click="closePopup(false)"></div>
    <div class="edit_box" v-loading="loading">
      <div class="box_head">
        <p>{{typeId?'修改':'新增'}}商品类型</p>
        <i  @click="closePopup(false)"></i>
      </div>
      <div class="box_body" >
        <div class="line" :class="{'error_border':tip_num===1}">
          <span class="lab"><i class="mark_icon">*</i>名称：</span>
          <div>
            <el-input type="text" placeholder="请输入名称"  v-model.trim="proName"
                      @input="setTip('','')"></el-input>
          </div>
        </div>
        <div class="line" :class="{'error_border':tip_num===2}">
          <span class="lab"><i class="mark_icon">*</i>图片：</span>
          <div>
            <el-upload
              :data="param"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="avatarSuccess"
              :before-upload="avatarUpload">
              <img v-if="photoUrl" :src="photoUrl" class="avatar">
              <i class="el-icon-plus avatar-uploader-icon" v-else></i>
            </el-upload>
          </div>
        </div>
        <div class="line" :class="{'error_border':tip_num===3}">
          <span class="lab"><i class="mark_icon">*</i>排序：</span>
          <div>
            <el-input type="number" placeholder="请输入排序"  v-model.trim="sort"
                      @input="setTip('','')" min="1"></el-input>
          </div>
        </div>
        <div class="line">
          <span class="lab">标注：</span>
          <div>
            <el-input type="text" placeholder="请输入标注"  v-model.trim="memo"
                      @input="setTip('','')"></el-input>
          </div>
        </div>
        <div class="line">
          <span class="lab">是否展示：</span>
          <div>
            <el-radio-group v-model="isDisplay">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </div>
        </div>
        <p class="error_line">{{infoTip}}</p>
      </div>
      <div class="box_foot">
        <span @click="closePopup(false)">取消</span>
        <span @click="confirmEdit">确定</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getDetail, addData, updateData } from "@/api/config/productType";
import {restUrl} from "../../../../url";
export default {
  props:['typeId'],
  data(){
    return {
      param: {folder: 'web/productType'},
      uploadUrl: restUrl + '/common/upload',
      sort: '',
      proName: '',
      photoUrl: '',
      memo:'',
      isDisplay:1,
      // proStatus: 1,
      infoTip: '',
      tip_num: '',
      loading:false
    }
  },
  mounted() {
    if (this.typeId){
      this.getDetail();
    }
  },
  methods:{
    getDetail(){
      let that = this;
      that.loading = true;
      getDetail(that.typeId).then(res => {
        if (res){
          that.setData(res);
        }
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    setData(obj){
      this.proName = obj.name?obj.name:'';
      this.sort = obj.sort?obj.sort:'';
      this.photoUrl = obj.photoUrl?obj.photoUrl:'';
      this.memo = obj.memo ?obj.memo:'';
      this.isDisplay = obj.isDisplay ?obj.isDisplay:0
      // this.proStatus = obj.enable ? obj.enable : 0;
    },

    // 上传图标
    avatarUpload(file) {
      this.setTip('', '');
      this.loading = true;
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isLt10M) {
        this.$message.error({
          message:'上传图像大小不能超过 10MB!',
          duration:1500
        });
      }
      return isLt10M;
    },

    // 图标上传回调
    avatarSuccess(res, file) {
      if (file.response.success) {
        this.photoUrl = file.response.data;
        this.$message.success({
          message:'上传成功！',
          duration:1500
        });
      } else {
        this.$message.error({
          message:'上传失败，请重试',
          duration:1500
        });
      }
      this.loading = false;
    },

    // 确认编辑
    confirmEdit(){
      if (this.proName === '') {
        this.setTip(1, '请输入类型名称！')
      }  else if (this.photoUrl ==='') {
        this.setTip(2, '请上传图片！');
      }else if (this.sort === '') {
        this.setTip(3, '请输入排序！');
      } else if (this.sort%1!==0) {
        this.setTip(3, '排序需为整数！');
      } else {
        this.sendInfo();
      }
    },

    sendInfo(){
      let that = this;
      that.loading = true;
      let data = {
        photoUrl: that.photoUrl,
        // enable: that.proStatus,
        sort: that.sort,
        name: that.proName,
        memo:that.memo,
        isDisplay:that.isDisplay
      }
      if (that.typeId) {
        data.id = that.typeId;
        updateData(JSON.stringify(data)).then(response => {
          that.closePopup(true);
          that.$message.success({
            message:'操作成功！',
            duration:1500
          });
          that.loading = false;
        }).catch((e) => {
          that.loading = false;
        });
      } else {
        addData(JSON.stringify(data)).then(response => {
          that.closePopup(true);
          that.$message.success({
            message:'操作成功！',
            duration:1500
          });
          that.loading = false;
        }).catch((e) => {
          that.loading = false;
        });
      }
    },

    setTip(num,text){
      this.tip_num = num;
      this.infoTip = text
    },

    // 关闭
    closePopup(refresh){
      this.$emit('listenToChild', {refresh: refresh})
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #60BAE7;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #DCDFE6;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
  border: 1px solid #DCDFE6;
  border-radius: 6px;
}

.avatar {
  max-width: 210px;
  min-width: 60px;
  height: 100px;
  display: block;
  border-radius: 6px;
  margin-right: 15px;
  border: 1px solid #bbb;
}
.avatar_icon{
  max-width: 130px;
  height: 40px;
  object-fit: cover;
  cursor: pointer;
}

</style>
